#include "VErrorWidget.h"
#include "ui_VErrorWidget.h"
#include "VGlobal.h"

#include <QFrame>
#include <QDebug>
#include <QIcon>
#include <QListView>
#include <QFileInfo>

VErrorWidget::VErrorWidget(QWidget *parent) :
    QFrame(parent),
    ui(new Ui::VErrorWidget)
{
    ui->setupUi(this);
    this->installEventFilter(this);

    _checkbox_error = new QCheckBox(this);
    _checkbox_error->setText("错误0");
    _checkbox_error->setChecked(true);
    _checkbox_warning = new QCheckBox(this);
    _checkbox_warning->setText("警告0");
    _checkbox_warning->setChecked(true);
    _checkbox_info = new QCheckBox(this);
    _checkbox_info->setText("信息0");
    _checkbox_info->setChecked(true);

    connect(_checkbox_error, &QCheckBox::toggled, this, &VErrorWidget::update_filter);
    connect(_checkbox_warning, &QCheckBox::toggled, this, &VErrorWidget::update_filter);
    connect(_checkbox_info, &QCheckBox::toggled, this, &VErrorWidget::update_filter);

    ui->table_error->setObjectName("tablewidget_qss");
    ui->table_error->setFocusPolicy(Qt::NoFocus);
    ui->table_error->verticalHeader()->hide();
    ui->table_error->setColumnWidth(0, 26);
    ui->table_error->setColumnWidth(1, 76);
    ui->table_error->setColumnWidth(6, 76);
    ui->table_error->setColumnHidden(3, true);  //隐藏当前
    ui->table_error->setColumnHidden(4, true);  //隐藏路径
    ui->table_error->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
    ui->table_error->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
    ui->table_error->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
    ui->table_error->horizontalHeader()->setSectionResizeMode(3, QHeaderView::Interactive);
    ui->table_error->horizontalHeader()->setSectionResizeMode(4, QHeaderView::Interactive);
    ui->table_error->horizontalHeader()->setSectionResizeMode(5, QHeaderView::Interactive);
    ui->table_error->horizontalHeader()->setSectionResizeMode(6, QHeaderView::Fixed);
}

VErrorWidget::~VErrorWidget()
{
    delete ui;
}

void VErrorWidget::table_add_line_data(int type, int code, QString content, QString current, QString path, int line)
{
    m_table_item_list.append(VTableItem(type, code, content, current, path, line));
    init_count();
    table_add_line(type, code, content, current, path, line);
}

void VErrorWidget::table_add_line_data(VErrorWidget::VTableItem data)
{
    m_table_item_list.append(data);
    init_count();
    table_add_line(data.type, data.code, data.content, data.current, data.path, data.line);
}

void VErrorWidget::table_clear()
{
    m_table_item_list.clear();
    while(ui->table_error->rowCount()>0)
    {
        ui->table_error->removeRow(0);
    }

}

void VErrorWidget::on_table_error_objectNameChanged(const QString &objectName)
{
    ui->table_error->resizeRowsToContents();
}

void VErrorWidget::update_filter()
{
    VErrorWidget::table_update();
}

bool VErrorWidget::eventFilter(QObject *watched, QEvent *event)
{
    if (event->type() == QEvent::Resize)
    {
        ui->table_error->resizeRowsToContents();
    }
    return QFrame::eventFilter(watched, event);
}

void VErrorWidget::table_add_line(int type, int code, QString content, QString current, QString path, int line)
{
    QString icon_path;

    if(type == VTableItemType::INFO && _checkbox_info->isChecked())
    {
        icon_path = VGlobal::icon_path.value("info");
    }
    else if(type == VTableItemType::WARNING && _checkbox_warning->isChecked())
    {
        icon_path = VGlobal::icon_path.value("warning");
    }
    else if(type == VTableItemType::ERROR && _checkbox_error->isChecked())
    {
        icon_path  =VGlobal::icon_path.value("error");
    }
    else
    {
        return;
    }

    int row = ui->table_error->rowCount();
    ui->table_error->insertRow(row);

    QTableWidgetItem *item;
    // 图标
    item= new QTableWidgetItem(QIcon(icon_path), NULL);
    ui->table_error->setItem(row, 0, item);

    // code
    if(code >= 0)
    {
        item= new QTableWidgetItem(QString::number(code));
        ui->table_error->setItem(row, 1, item);
    }

    // content
    item= new QTableWidgetItem(content);
    ui->table_error->setItem(row, 2, item);

    // 路径
    item= new QTableWidgetItem(current);
    ui->table_error->setItem(row, 3, item);

    // 文件名
    item= new QTableWidgetItem(path);
    ui->table_error->setItem(row, 4, item);

    // name
    item= new QTableWidgetItem(QFileInfo(path).fileName());
    ui->table_error->setItem(row, 5, item);

    // line
    if(line >= 0)
    {
        item= new QTableWidgetItem(QString::number(line));
        ui->table_error->setItem(row, 6, item);
    }


    ui->table_error->resizeRowsToContents();
}

void VErrorWidget::init_count()
{
    int error = 0;
    int warning = 0;
    int info = 0;

    foreach(VTableItem item, m_table_item_list)
    {
        switch (item.type) {
        case VTableItemType::ERROR:
            error++;
            break;
        case VTableItemType::WARNING:
            warning++;
            break;
        case VTableItemType::INFO:
            info++;
            break;
        }
    }

    _checkbox_error->setText(QString("错误 %1").arg(error));
    _checkbox_warning->setText(QString("警告 %1").arg(warning));
    _checkbox_info->setText(QString("信息 %1").arg(info));


}

void VErrorWidget::table_update()
{
    while(ui->table_error->rowCount()>0)
    {
        ui->table_error->removeRow(0);
    }

    foreach(VTableItem data, m_table_item_list)
    {
        table_add_line(data.type, data.code, data.content, data.current, data.path, data.line);
    }
}


void VErrorWidget::on_table_error_doubleClicked(const QModelIndex &index)
{
    qDebug() << index.row();
    int row = index.row();
    QString current = ui->table_error->item(row, 3)->text();
    QString path = ui->table_error->item(row, 4)->text();
    qDebug() << current + "/" + path;
    if(QFile(current + "/" + path).exists() == false) return;
    if(ui->table_error->item(row, 6) == nullptr) return;
    if(ui->table_error->item(row, 6)->text().isEmpty()) return;
    int line = ui->table_error->item(row, 6)->text().toInt();
    emit signal_jump(current + "/" + path, line);
}
